package com.app.couponsharelife.ui.main.me.team

import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.databinding.ActivityTeamBinding
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.adapter.ViewPagerAdapter
import com.app.couponsharelife.ui.viewmodel.MeViewModel
import com.app.couponsharelife.utils.AppUtils
import com.app.fuquan.data.bean.PageStatus
import kotlinx.android.synthetic.main.activity_team.*
import kotlinx.android.synthetic.main.common_tool_bar_layout.*
import java.util.HashMap

class TeamActivity : BaseActivity() {

    override val viewLayoutId: Int get() = R.layout.activity_team

    private var mAdapter: ViewPagerAdapter? = null

    private var mTitles = arrayListOf<String>()

    private var mPagers = arrayListOf<Fragment>()

    private var mViewModel: MeViewModel? = null

    override fun initViews() {
        initToolBar()
        initClickEvent()
        initViewPager()
    }


    private fun initToolBar() {
        ivBack.setOnClickListener { finish() }
        tvToolBarTitle.text = "我的团队"
    }

    private fun initClickEvent() {
        (mViewDataBinding as ActivityTeamBinding).listener = View.OnClickListener {
            when (it.id) {
                R.id.tvTotalTeamMate -> mViewPager.currentItem = 0
                R.id.tvDirectTeamMate -> mViewPager.currentItem = 1
                R.id.tvIndirectTeamMate -> mViewPager.currentItem = 2
            }
        }
    }

    private fun initViewPager() {
        with(mTitles) {
            add("全部队员")
            add("直属队员")
            add("推荐队员")
        }
        with(mPagers) {
            add(TeamFragment.newInstance(""))
            add(TeamFragment.newInstance("DIRECTLY_UNDER"))
            add(TeamFragment.newInstance("RECOMMEND"))
        }
        mAdapter = ViewPagerAdapter(supportFragmentManager, mPagers, mTitles)
        mViewPager.adapter = mAdapter
        mViewPager.offscreenPageLimit = 3
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                when (p0) {
                    0 -> {
                        tvTotalTeamMate.setBackgroundResource(R.drawable.ic_act_team_one_select_true)
                        tvDirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_two_select_false)
                        tvIndirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_three_select_false)
                    }
                    1 -> {
                        tvTotalTeamMate.setBackgroundResource(R.drawable.ic_act_team_one_select_false)
                        tvDirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_two_select_true)
                        tvIndirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_three_select_false)
                    }
                    2 -> {
                        tvTotalTeamMate.setBackgroundResource(R.drawable.ic_act_team_one_select_false)
                        tvDirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_two_select_false)
                        tvIndirectTeamMate.setBackgroundResource(R.drawable.ic_act_team_three_select_true)
                    }
                }
            }
        })
    }

    override fun initViewModel() {
        if (mViewModel == null) mViewModel = ViewModelProviders.of(this).get(MeViewModel::class.java)
        if (!mViewModel!!.getUserTeamInfo()!!.hasObservers()) {
            mViewModel!!.getUserTeamInfo()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserTeamInfo(hashMapOf(), false)
                        showToastMsg(parseErrorDate(it.error!!.message).tip)
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserTeamInfo(hashMapOf(), false)
                        showToastMsg("数据出错啦")
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetUserTeamInfo(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            Glide.with(this).asBitmap()
                                    .apply(RequestOptions().circleCrop()
                                            .placeholder(R.drawable.ic_tribe_boy_thumb)
                                            .error(R.drawable.ic_tribe_boy_thumb))
                                    .load(it.data.data!!.headimgurl)
                                    .into(ivUserImage)
                            tvUserName.text = it.data.data!!.nickName
                            tvUserUpName.text = String.format("推荐我的人：%s", it.data.data!!.parentNickName)
                            tvUserTotalTeamMateNum.text = String.format("全部队员：%s", it.data.data!!.directlyUnderCount + it.data.data!!.recommendCount)
                            tvUserDirectTeamMateNum.text = String.format("直属队员：%s", it.data.data!!.directlyUnderCount)
                            tvUserIndirectTeamMateNum.text = String.format("推荐队员：%s", it.data.data!!.recommendCount)
                        } else {
                            showToastMsg("获取失败")
                        }
                    }
                }
            })
        }
        loadData()
    }

    private fun loadData() {
        val map = HashMap<String, String>()
        map["userId"] =AppUtils.getString(Contacts.USERID, "")
        mViewModel?.freshGetUserTeamInfo(map, true)
    }
}
